unit clsMovItemPedCompra;

interface

uses Dm,dbtables,SysUtils,biblio;

type
  TMovItemPedCompra=Class(TObject)
    public
      Pro_Codigo: string;
      quantidade: double;
      function  Existe_Movto(Pedc_Numero,Ent_Codigo,Pedc_Item: string): boolean;
      function  Existe_Movtos(Pedc_Numero,Ent_Codigo: string): boolean;
      function  Recuperar(var pedido1,Pedc_Item: string; Ent_Codigo,Notac_Numero,Notc_Serie,Notc_Item: String): Boolean;
      procedure Retornar_Nota(Pedc_Numero,Ent_Codigo: string; var Notac_Numero,Notc_Serie: string);
      procedure Gravar(Pedc_Numero,Ent_Codigo,Pedc_Item,Notac_Numero,Notc_Serie,Notc_Item: String);
      procedure Excluir(Ent_Codigo,Notac_Numero,Notc_Serie: String);
      function  Retorna_Pedido(P_Nota_serie,P_nota_numero,P_ent_codigo: string): string;
    end;

implementation

procedure TMovItemPedCompra.Gravar;
var tq: TQuery;
begin
 tq             := Tquery.Create(DmInt);
 tq.databasename:= dmint.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from MovItemPedCompra where Pedc_Numero=:a and Ent_Codigo=:b and '+
            'Pedc_Item=:c and Notac_Numero=:d and Notc_Serie=:e and Notc_Item=:f');
 tq.Params[0].asstring:= Pedc_Numero;
 tq.params[1].asstring:= strzero(Ent_Codigo,5);
 tq.params[2].asstring:= Pedc_Item;
 tq.params[3].asstring:= Notac_Numero;
 tq.params[4].asstring:= Notc_Serie;
 tq.params[5].asstring:= Notc_Item;
 tq.open;
 if tq.eof then begin
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into MovItemPedCompra(PEDC_NUMERO, ENT_CODIGO, PEDC_ITEM, NOTAC_NUMERO, '+
             'NOTC_SERIE, NOTC_ITEM, PRO_CODIGO, QUANTIDADE) values('+
             ':k_Pedc_Numero,'+
             ':k_Ent_Codigo,'+
             ':k_Pedc_Item,'+
             ':k_Notac_Numero,'+
             ':k_Notc_Serie,'+
             ':k_Notc_Item,'+
             ':k_Pro_Codigo,'+
             ':k_Quantidade)');
 end else begin
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Updape MovItemPedCompra set '+
             'Pro_Codigo=:k_Pro_Codigo,'+
             'Quantidade=:k_Quantidade '+
             'where '+
             'Pedc_Numero =:k_Pedc_Numero and '+
             'Ent_Codigo  =:k_Ent_Codigo and '+
             'Pedc_Item   =:k_Pedc_Item and '+
             'Notac_Numero=:k_Notac_Numero and '+
             'Notc_Serie  =:k_Notc_Serie and '+
             'Notc_Item   =:k_Notc_Item');
 end;
 tq.ParamByName('k_Pedc_Numero').asstring := Pedc_Numero;
 tq.ParamByName('k_Ent_Codigo').asstring  := strzero(Ent_Codigo,5);
 tq.ParamByName('k_Pedc_Item').asstring   := Pedc_Item;
 tq.ParamByName('k_Notac_Numero').asstring:= Notac_Numero;
 tq.ParamByName('k_Notc_Serie').asstring  := Notc_Serie;
 tq.ParamByName('k_Notc_Item').asstring   := Notc_Item;
 tq.ParamByName('k_Pro_Codigo').asstring  := Pro_Codigo;
 tq.ParamByName('k_Quantidade').asfloat   := Quantidade;
 tq.close;
 tq.execsql;
 tq.close;
 tq.Free;
end;

function TMovItemPedCompra.Recuperar;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= DmInt.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.sql.add('Select * from MovItemPedCompra where Ent_Codigo=:b and Notac_Numero=:d and '+
            'Notc_Serie=:e and Notc_Item=:f');
 tq.params[0].asstring:= strzero(Ent_Codigo,5);
 tq.params[1].asstring:= Notac_Numero;
 tq.params[2].asstring:= Notc_Serie;
 tq.params[3].asstring:= Notc_Item;
 tq.open;
 if tq.eof then result:= false else result:= true;
 pedido1  := tq.fieldbyname('Pedc_Numero').asstring;
 Pedc_Item:= tq.fieldbyname('Pedc_Item').asstring;
 tq.next;
 tq.Close;
 tq.Free;
end;

procedure TMovItemPedCompra.Excluir;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Delete from MovItemPedCompra where Ent_Codigo=:b and Notac_Numero=:d and '+
            'Notc_Serie=:e');
 tq.params[0].asstring:= strzero(Ent_Codigo,5);
 tq.params[1].asstring:= Notac_Numero;
 tq.params[2].asstring:= Notc_Serie;
 tq.execSQL;
 tq.close;
 tq.Free;
end;

function TMovItemPedCompra.Existe_Movto;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= DmInt.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.sql.add('Select * from MovItemPedCompra where Pedc_Numero=:a and Ent_Codigo=:b and '+
            'Pedc_Item=:c');
 tq.Params[0].asstring:= Pedc_Numero;
 tq.params[1].asstring:= strzero(Ent_Codigo,5);
 tq.params[2].asstring:= Pedc_Item;
 tq.open;
 if tq.eof then result:= false else result:= true;
 tq.Close;
 tq.Free;
end;

function TMovItemPedCompra.Existe_Movtos;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= DmInt.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.sql.add('Select * from MovItemPedCompra where Pedc_Numero=:a and Ent_Codigo=:b');
 tq.Params[0].asstring:= Pedc_Numero;
 tq.params[1].asstring:= strzero(Ent_Codigo,5);
 tq.open;
 if tq.eof then result:= false else result:= true;
 tq.Close;
 tq.Free;
end;

function TMovItemPedCompra.Retorna_Pedido;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from MovItemPedCompra where Notc_Serie=:a and Notac_Numero=:b and '+
            'Ent_Codigo=:c');
 tq.Params[0].asstring:= p_Nota_Serie;
 tq.Params[1].asstring:= p_Nota_Numero;
 tq.Params[2].asstring:= p_Ent_Codigo;
 tq.open;
 result:= tq.fieldbyname('Pedc_Numero').asstring;
 tq.close;
 tq.free;
end;

procedure TMovItemPedCompra.Retornar_Nota;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from MovItemPedCompra where Pedc_Numero=:a and Ent_Codigo=:b');
 tq.Params[0].asstring:= Pedc_Numero;
 tq.Params[1].asstring:= Ent_Codigo;
 tq.open;
 Notac_Numero:= tq.fieldbyname('Notac_Numero').asstring;
 Notc_Serie  := tq.fieldbyname('Notc_Serie').asstring;
 tq.close;
 tq.free;
end;

end.

